﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Insteon.Messages.Insteon
{
    class StandardResponse : Message
    {
        internal const int MessCommand = 0x62;
        internal const int MessLength = 9;
        internal const int MessFlagsByte = 5;


        internal readonly InsteonAddress To;
        internal readonly byte Command1;
        internal readonly byte Command2;
        internal readonly Flags Flags;
        internal readonly ResponseStatus Status;

        public StandardResponse(byte[] buffer, UInt16 Length, UInt64 Number, out UInt16 Size)
            : base(buffer, Length, Number)
        {
            if (!Check(buffer, Length))
            {
                throw new Exception("Invalid message format");
            }
            To = new InsteonAddress(buffer, 2);
            Flags = new Flags(buffer[MessFlagsByte]);
            Command1 = buffer[6];
            Command2 = buffer[7];            
            Status = new ResponseStatus(buffer[8]);
            Size = MessLength;
        }

        internal static bool Check(byte[] buffer, UInt16 length)
        {
            if ((length >= MessLength) && (buffer[1] == MessCommand))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override string ToString()
        {
            return PrintCommonMessage(this.ReceivedTime, Number, this, To, null, Flags, Command1, Command2, Status);
        }

    }
}
